package org.grayrabbit.cms.util.lucene;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.DateTools.Resolution;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.vo.lucene.LuceneBean;
import org.jsoup.Jsoup;


public class LuceneUtil {
	public final static String DBID = "id";
	public final static String ENTITYNAME = "entityName";
	public final static String URL = "url";
	public final static String TITLE = "title";
	public final static String TYPE = "type";
	public final static String TYPE_HTML = "0";
	public final static String Type_DB = "1";

	public final static String CONTENT = "content";
	public final static String DATE = "date";
	public final static String SITEID = "siteId";
	public final static String SITENAME = "siteName";
	public final static String SOURCE = "source";
	public final static String AUTHOR = "author";
	public final static String CATEGORYID = "categoryId";
	public final static String CATEGOEYNAME = "categoryName";
	public final static String TOPICID = "topicId";
	public final static String TOPICNAME = "topicName";

	public final static String CONTENTTYPE = "contentType";
	public final static String CONTENTTYPE_VIDEO = "VideoContent";
	public final static String CONTENTTYPE_IMAGE = "PictureContent";
	public final static String CONTENTTYPE_COMMON = "Content";
	public static Document getDocument(LuceneBean l) throws Exception {
		Document doc = new Document();
		try{
			doc.add(new Field(DBID, StringUtils.trimToEmpty(l.getCategoryId()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(ENTITYNAME, StringUtils.trimToEmpty(l.getEntityName()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(URL, l.getUrl(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TITLE, StringUtils.trimToEmpty(l.getTitle()), Store.YES, Index.ANALYZED));
			if(StringUtils.isBlank(l.getType())){
				l.setType(Type_DB);
			}
			doc.add(new Field(TYPE, l.getType() , Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CONTENT, StringUtils.trimToEmpty(l.getContent()), Store.YES, Index.ANALYZED));
			doc.add(new Field(DATE, DateTools.dateToString(l.getDate(),Resolution.SECOND), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITEID, StringUtils.trimToEmpty(l.getSiteId()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITENAME, StringUtils.trimToEmpty(l.getSiteName()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SOURCE, StringUtils.trimToEmpty(l.getSource()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(AUTHOR, StringUtils.trimToEmpty(l.getAuthor()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGORYID, StringUtils.trimToEmpty(l.getCategoryId()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGOEYNAME, StringUtils.trimToEmpty(l.getCategoryName()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TOPICID, StringUtils.trimToEmpty(l.getTopicId()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TOPICNAME, StringUtils.trimToEmpty(l.getTopicName()), Store.YES, Index.NOT_ANALYZED));
			
			if(StringUtils.isBlank(l.getContentType())){
				l.setContentType(CONTENTTYPE_COMMON);
			}
			doc.add(new Field(CONTENTTYPE, l.getContentType(), Store.YES, Index.NOT_ANALYZED));
		}
		catch (Exception e) {
			throw new Exception("LuceneBean->Document转换异常", e);
		}
		return doc;
	}

	public static Document getDocument(Content l,Site site,String categoryid,String categoryName,Topic topic) throws Exception {
		Document doc = new Document();
		try{
			doc.add(new Field(DBID, l.getContentId(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(ENTITYNAME, "content", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(URL, site.getSiteDomain()+(site.getSiteDomain().endsWith("/")?"":"/")+l.getPublishUrl(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TITLE, StringUtils.trimToEmpty(l.getContentTitle()), Store.YES, Index.ANALYZED));
			doc.add(new Field(TYPE, Type_DB , Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CONTENT, HtmlUtil.getJsoupContent(l.getContentContext()), Store.YES, Index.ANALYZED));
			doc.add(new Field(DATE, DateTools.dateToString(l.getPublishTime(),Resolution.SECOND), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITEID, site.getSiteId(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITENAME, site.getSiteName(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SOURCE, StringUtils.trimToEmpty(l.getContentSource()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(AUTHOR, StringUtils.trimToEmpty(l.getContentAuthor()), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGORYID, StringUtils.trimToEmpty(categoryid), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGOEYNAME, StringUtils.trimToEmpty(categoryName), Store.YES, Index.NOT_ANALYZED));
			if(topic!=null){
				doc.add(new Field(TOPICID, topic.getTopicId(), Store.YES, Index.NOT_ANALYZED));
				doc.add(new Field(TOPICNAME, topic.getTopicName(), Store.YES, Index.NOT_ANALYZED));
			}
			else{
				doc.add(new Field(TOPICID, "", Store.YES, Index.NOT_ANALYZED));
				doc.add(new Field(TOPICNAME, "", Store.YES, Index.NOT_ANALYZED));
			}
			doc.add(new Field(CONTENTTYPE, l.getContextType(), Store.YES, Index.NOT_ANALYZED));
			if(l.getIsTop()!=null&&l.getIsTop()){
				doc.setBoost(1.1f);//改变得分，排在前面
			}
		}
		catch (Exception e) {
			throw new Exception("Content->Document转换异常", e);
		}
		return doc;
	}
	
	
	
	public static Document getDocument(File htmlFile,String path,Site site) throws Exception {
		Document doc = new Document();
		try{
			org.jsoup.nodes.Document jsoupdoc = Jsoup.parse(htmlFile, "UTF-8");
			doc.add(new Field(DBID, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(ENTITYNAME, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(URL, site.getSiteDomain()+(site.getSiteDomain().endsWith("/")?"":"/")+path, Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TITLE, StringUtils.trimToEmpty(jsoupdoc.title()), Store.YES, Index.ANALYZED));
			doc.add(new Field(TYPE, TYPE_HTML , Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CONTENT, jsoupdoc.body().text(), Store.YES, Index.ANALYZED));
			doc.add(new Field(DATE, DateTools.dateToString(new Date(htmlFile.lastModified()),Resolution.SECOND), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITEID, site.getSiteId(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SITENAME, site.getSiteName(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(SOURCE, site.getSiteName(), Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(AUTHOR, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGORYID, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CATEGOEYNAME, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TOPICID, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(TOPICNAME, "", Store.YES, Index.NOT_ANALYZED));
			doc.add(new Field(CONTENTTYPE,"", Store.YES, Index.NOT_ANALYZED));
		}
		catch (Exception e) {
			throw new Exception("HTMLFile->Document转换异常", e);
		}
		return doc;
	}
	
	public static LuceneBean getLuceneBean(Document doc) throws Exception{
		LuceneBean lb = new LuceneBean();
		try{
			lb.setId(DBID);
			lb.setEntityName(doc.get(ENTITYNAME));
			lb.setUrl(doc.get(URL));
			lb.setTitle(doc.get(TITLE));
			lb.setType(doc.get(TYPE));
			lb.setContent(doc.get(CONTENT));
			lb.setDate(DateTools.stringToDate(doc.get(DATE)));
			lb.setSiteId(doc.get(SITEID));
			lb.setSiteName(doc.get(SITENAME));
			lb.setSource(doc.get(SOURCE));
			lb.setAuthor(doc.get(AUTHOR));
			lb.setCategoryName(doc.get(CATEGOEYNAME));
			lb.setCategoryId(doc.get(CATEGORYID));
			lb.setTopicId(doc.get(TOPICID));
			lb.setTopicName(doc.get(TOPICNAME));
			lb.setContentType(doc.get(CONTENTTYPE));
			return lb;
		}
		catch (Exception e) {
			throw new Exception("Document->LuceneBean 转换异常", e);
		}
		
	}
	
	
}
